package com.ugoodtech.mdcc.client.controller;

import com.ugoodtech.mdcc.client.service.WeChatCallbackService;
import java.io.BufferedReader;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Xiangcheng Li
 *
 * @author Xiangcheng Li
 */

@RestController
public class WeChatController {


    @Autowired
    private WeChatCallbackService weChatCallbackService;

    @RequestMapping(value = "/wechat/callback", method = RequestMethod.GET)
    public Long communityGroupsVerifyUrl(@RequestParam String signature,
        @RequestParam String timestamp,
        @RequestParam String nonce, @RequestParam(required = false) String echostr, HttpServletRequest request)
        throws IOException {
        System.out.println(request.getRequestURL().toString());
        StringBuilder body = new StringBuilder();
        try (BufferedReader reader = request.getReader()) {
            String line;
            while ((line = reader.readLine()) != null) {
                body.append(line);
            }
        }

        System.out.println(body);
        StringBuffer requestURL = request.getRequestURL();
        String queryString = request.getQueryString();

        if (queryString != null) {
            requestURL.append("?").append(queryString);
        }

        String fullURL = requestURL.toString();
        System.out.println(fullURL);
        return Long.valueOf(weChatCallbackService.verifyUrl(echostr, timestamp, nonce, signature));
    }


    /**
     * 设置公众号菜单
     *
     * @param link
     * @return
     */
    @RequestMapping(value = "/wechat/callback/menu", method = RequestMethod.GET)
    public void addMenu(@RequestParam(name = "link") String link){
        weChatCallbackService.addMenu(link);
    }

    @RequestMapping(value = "/wechat/callback", method = RequestMethod.POST)
    public String handleCallback(HttpServletRequest request, @RequestParam String msg_signature,
        @RequestParam String timestamp,
        @RequestParam String nonce,  String reqBody) throws IOException {
        System.out.println(request.getRequestURL().toString());
        StringBuilder body = new StringBuilder();
        try (BufferedReader reader = request.getReader()) {
            String line;
            while ((line = reader.readLine()) != null) {
                body.append(line);
            }
        }

        System.out.println(body);
        StringBuffer requestURL = request.getRequestURL();
        String queryString = request.getQueryString();

        if (queryString != null) {
            requestURL.append("?").append(queryString);
        }

        String fullURL = requestURL.toString();
        System.out.println(fullURL);
        weChatCallbackService.handleCallback(body.toString(), timestamp, nonce, msg_signature);
        return "";
    }

    @RequestMapping(value = "/wechat/openid/history", method = RequestMethod.GET)
    public String getUnionIdOfOldOpenId(HttpServletRequest request) throws IOException {

        weChatCallbackService.getUnionIdOfOldOpenId();
        return "";
    }

}
